﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.DataAccess.Entities;
using CRM.Business.Services;
using CRM.Encryption;
using Telerik.Web.UI;
using System.IO;

namespace Web.Terms_And_Conditions
{
    public partial class EditTermsAndConditions : System.Web.UI.Page
    {
        static string prevPage = String.Empty;
        static TERMS_AND_CONDITIONS TAC;
        static long tacID;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;
            if (Request.UrlReferrer != null)
            {
                prevPage = Request.UrlReferrer.ToString();
                hdntxtPrevPage.Value = prevPage;
            }
            string requestEdit = Request.QueryString["id"];
            if (requestEdit != null)
            {
                tacID = Convert.ToInt64(requestEdit);
                LoadDataTACEdit();
            }
        }
        private void LoadDataTACEdit()
        {
            TAC = TermsAndConditionsService.GetTACByID(tacID);
            if (TAC != null)
            {
                txtDescription.Text = TAC.DESCRIPTION;
                txtTACName.Text = TAC.NAME;
            }
            else
            {
                divContent.Style["display"] = "none";
                divError.Style["display"] = "block";
            }
        }
        private int updateTAC()
        {
            TAC.NAME = txtTACName.Text.Trim();
            TAC.DESCRIPTION = txtDescription.Text.Trim();
            return TermsAndConditionsService.UpdateTAC(TAC);
        }
        protected void btn_Save_Click(object sender, EventArgs e)
        {
            int updateT = updateTAC();
            if (updateT == 1)
            {
                RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessageAndRedirect('" + "Update TAC successful !" + "','" + "TermsAndConditions.aspx" + "');");
                //MessageBox.Show(this.Page, Page.GetType(), "Update Role successful !", "Roles.aspx");
            }
            else
                RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessage('" + "Can not update this TAC !" + "');");
            //MessageBox.Show(this.Page, Page.GetType(), "Can not update Role !", "");

        }
        protected void btn_Cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(prevPage);
        }
        protected void btn_Delete_ServerClick(object sender, EventArgs e)
        {
            TERMS_AND_CONDITIONS tac = TermsAndConditionsService.GetTACByID(tacID);
            int tacDel = TermsAndConditionsService.DeleteTAC(tacID);
            if (tacDel == 1)
            {
                RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessageAndRedirect('" + "Delete TAC successful !" + "','" + "TermsAndCondtions.aspx" + "');");
                //MessageBox.Show(this.Page, Page.GetType(), "Delete ROLE successful !", "Roles.aspx");
            }
            else
                RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessage('" + "Can not delete this TAC !" + "');");
            //MessageBox.Show(this.Page, Page.GetType(), "Can not delete ROLE !", "");

        }
    }
}